<?php

class orderController extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $order = Model_TableOrder::getInstance();
        $available = $order->selectAvailableTable();
        $occupied = $order->selectOccupiedTable();
        $payed = $order->selectPayedTable();

        $this->view->render('order/index', array('available' => $available, 'occupied' => $occupied, 'payed' => $payed));
    }

    function addOrder($params = array()) {
        $tid = _get($params, "tid");
        $db_table = Model_Table::getInstance();
        $db_menu = Model_Menu::getInstance();
        $data['tid'] = $tid;
        $data['table'] = $db_table->select("all", array('id' => $tid));
        if (is_object($data['table'])) {
            $data['menu'] = $db_menu->selectAll();
            $this->view->render('order/addOrder', $data);
        } else {
            $this->index();
        }
    }

    function addMenu($params = array()) {
        $tid = _get($params, "tid");
        $mid = _get($params, "mid");

        $db_tableOrder = Model_TableOrder::getInstance();
        $db_order = Model_Order::getInstance();
        // id tableOrder 
        $table_id = $db_tableOrder->set_new_tableOrder($tid);
        if ($db_order->check_existed_menu($table_id, $mid) == true) {
            $insert = $db_order->update_amount($table_id, $mid);
        } else {
            $insert = $db_order->insert(array('menu_id' => $mid, 'table_id' => $table_id, 'amount' => 1));
        }

        $this->view->redirect('/order/resume', array('tid' => $tid));
    }

    function resume($params = array()) {
        $tid = _get($params, "tid");
        $db_tableOrder = Model_TableOrder::getInstance();
        $db_Order = Model_Order::getInstance();
        $table = $db_tableOrder->selectCurrentTable($tid);
        $menu = $db_Order->getMenuByTid($table->tableOrderId);
        $data = array('menu' => $menu, 'table' => $table);
        $this->view->render('order/resume', $data);
    }

    function addition($params = array()) {
        $tid = _get($params, "tid");
        $db_tableOrder = Model_TableOrder::getInstance();
        $db_Order = Model_Order::getInstance();
        $table = $db_tableOrder->selectCurrentTable($tid);
        $menu = $db_Order->getMenuByTid($table->tableOrderId);
        $table->total = $db_tableOrder->getTotalByTid($table->tableOrderId)->total;
        $data = array('menu' => $menu, 'table' => $table);

        $this->view->render('order/addition', $data);
    }

    function deal($params = array()) {
        $tid = _get($params, "tid");
        $db_tableOrder = Model_TableOrder::getInstance();
        $db_tableOrder->setTableStatus($tid, 0);

        $this->index();
    }

    function listMenu($params = array()) {
        $db_menus = Model_Menu::getInstance();
        $data['tid'] = _get($params, 'tid');
        $type = _get($params, 'type');
        $data['menus'] = (object)$db_menus->select('all',array('type' => $type),PDO::FETCH_OBJ,true);
        
        $this->view->setInclude(false, false);
        $this->view->render('/order/listMenu', $data);
    }
}

?>
